﻿$(document).ready(function () {
    $('#explore-link').click(function () {
        $(this).addClass('active');
        $('#explore-menu').show();
        return false
    }).blur(function () {
        $(this).removeClass('active');
        $('#explore-menu').hide();
    });
    $('#search-field').focus(function () {
        if ($(this).attr('value') == $(this).attr('alt'))
            $(this).val('');
    }).blur(function () {
        if ($(this).val() == '')
            $(this).val($(this).attr('alt'));
    });
    $('.border').hover(function () {
        $(this).addClass('bhover');
    }, function () {
        $(this).removeClass('bhover');
    });
    $(window).scroll(function () {
        $('.ui-dialog').animate({ top: $(document).scrollTop() + ($(window).height() - $('.ui-dialog').height()) / 2 }, { duration: 600, queue: false });
    });
});

function show(url, width) {
    var dframe = $('#dialog-container').attr('src', url);
    dframe.load(function () {
        $(this).dialog({
            autoOpen: true,
            modal: true,
            width: width,
            zIndex: 100001,
            overlay: { opacity: 0.5, background: 'black' },
            title: dframe.contents().find('title').html()
        });
        $(this).width(width).height($(this).contents().height());
        $('.ui-dialog').animate({ top: $(document).scrollTop() + ($(window).height() - $('.ui-dialog').height()) / 2 }, { duration: 600, queue: false });
    });
}

function closeModal() {
    $("#dialog-container").dialog("close");
}

/**
* Methods for creating, reading, and deleting cookies.
*/
var Cookie = {

    /**
    * Cached cookies.
    */
    cache: {},

    /**
    * Create a cookie. Can accept a third parameter as a literal object of options.
    *
    * @param key
    * @param value
    * @param options
    */
    create: function (key, value, options) {
        options = $.extend({}, options);
        options.expires = options.expires || 1; // Default expiration: 1 hour

        if (typeof options.expires == 'number') {
            var hours = options.expires;
            options.expires = new Date();
            options.expires.setTime(options.expires.getTime() + (hours * 3600000));
        }

        var cookie = [
			encodeURIComponent(key) + '=',
			options.escape ? encodeURIComponent(value) : value,
			options.expires ? '; expires=' + options.expires.toUTCString() : '',
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		];

        document.cookie = cookie.join('');

        if (Cookie.cache) {
            if (options.expires.getTime() < (new Date()).getTime())
                delete Cookie.cache[key];
            else
                Cookie.cache[key] = value;
        }
    },

    /**
    * Read a cookie.
    *
    * @param key
    * @return string
    */
    read: function (key) {
        // Use cache when available
        if (Cookie.cache[key])
            return Cookie.cache[key];

        var cache = {};
        var cookies = document.cookie.split(';');

        if (cookies.length > 0) {
            for (var i = 0; i < cookies.length; i++) {
                var parts = cookies[i].split('=');

                if (parts.length >= 2)
                    cache[$.trim(parts[0])] = parts[1];
            }
        }

        Cookie.cache = cache;
        return cache[key] || null;
    },

    /**
    * Delete a cookie.
    *
    * @param key
    */
    erase: function (key, options) {
        if (!options) {
            options = { expires: -1 };

        } else if (!options.expires) {
            options.expires = -1;
        }

        Cookie.create(key, 0, options);
    },

    /**
    * Returns whether cookies are supported/enabled by the browser
    *
    * @return boolean
    */
    isSupported: function () {
        return (document.cookie.indexOf('=') != -1);
    }
};